<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('修改答案')"/>
    <th:block th:include="include :: summernote-css"/>
</head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <div class="row">
        <div class="col-sm-4">
            <label>题目信息</label>
            <div class="wrapper wrapper-content animated fadeInRight ibox-content">
                [(${question.fullContent})]
            </div>
        </div>

        <div class="col-sm-8">
            <form class="form-horizontal m" id="form-answer-edit" th:object="${answer}">
                <input id="id" name="id" th:field="*{id}" type="hidden">
                <input type="text" name="questionId" hidden="true" th:value="${question.id}"/>

                <div class="form-group">
                    <label class="col-sm-2 control-label">答案：</label>
                    <div class="col-sm-10">
                        <input id="answer" name="answer" type="hidden" th:field="*{answer}" required>
                        <div id="editor-answer" class="summernote-answer"></div>
                    </div>
                </div>

                <div class="form-group">
                    <label class="col-sm-2 control-label">解析：</label>
                    <div class="col-sm-10">
                        <input id="analysis" name="analysis" type="hidden" th:field="*{analysis}">
                        <div id="editor-analysis" class=" summernote-analysis">
                        </div>
                    </div>
                </div>

                <div class="form-group">
                    <label class="col-sm-2 control-label">编辑辅助工具网站：</label>
                    <div class="btn-group" role="group">
                        <button type="button" class="btn btn-default" onclick="mathEdit()">数学公式编辑</button>
                    </div>
                </div>

                <div class="form-group">
                    <label class="col-sm-3 control-label">使用状态：</label>
                    <div class="col-sm-8">
                        <div class="radio-box"
                             th:each="dict : ${@sysDictDataServiceImpl.listDictDataByTypeSign('sys_use_status')}">
                            <input type="radio" th:id="${dict.id}" name="use" th:value="${dict.dataValue}"
                                   th:field="*{use}">
                            <label th:for="${dict.id}" th:text="${dict.dataKey}"></label>
                        </div>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-3 control-label">备注：</label>
                    <div class="col-sm-8">
                        <textarea id="remark" name="remark" class="form-control">[[*{remark}]]</textarea>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>
<th:block th:include="include :: footer"/>
<th:block th:include="include :: summernote-js"/>
<script type="text/javascript">
    const prefix = ctx + "tiku/answer";

    $(function () {
        $('.summernote-answer').summernote({
            placeholder: '请输入题目内容，请注意格式',
            height: 350,
            lang: 'zh-CN',
            followingToolbar: false,
            callbacks: {
                onImageUpload: function (files) {
                    sendFile(files[0], this);
                },
                onPaste: function (ne) {
                    const bufferText = ((ne.originalEvent || ne).clipboardData || window.clipboardData).getData('Text/plain');
                    ne.preventDefault ? ne.preventDefault() : (ne.returnValue = false);
                    document.execCommand("insertText", false, bufferText);
                }
            }
        });
        var content = $("#answer").val();
        $('#editor-answer').summernote('code', content);
    });

    $(function () {
        $('.summernote-analysis').summernote({
            placeholder: '请输入题目内容，请注意格式',
            height: 350,
            lang: 'zh-CN',
            followingToolbar: false,
            callbacks: {
                onImageUpload: function (files) {
                    sendFile(files[0], this);
                },
                onPaste: function (ne) {
                    const bufferText = ((ne.originalEvent || ne).clipboardData || window.clipboardData).getData('Text/plain');
                    ne.preventDefault ? ne.preventDefault() : (ne.returnValue = false);
                    document.execCommand("insertText", false, bufferText);
                }
            }
        });
        var content = $("#analysis").val();
        $('#editor-analysis').summernote('code', content);
    });

    // 上传文件
    function sendFile(file, obj) {
        var data = new FormData();
        data.append("file", file);
        $.ajax({
            type: "POST",
            url: ctx + "common/upload",
            data: data,
            cache: false,
            contentType: false,
            processData: false,
            dataType: 'json',
            success: function (result) {
                if (result.status === web_status.SUCCESS) {
                    $(obj).summernote('editor.insertImage', result.body.url, result.body.fileName);
                } else {
                    $.modal.alertError(result.msg);
                }
            },
            error: function (error) {
                $.modal.alertWarning("图片上传失败。");
            }
        });
    }

    /* 亿图数学公式编辑*/
    function mathEdit() {
        $.modal.open("亿图片", "https://math.edrawsoft.cn/", '1200', '700');
    }

    function submitHandler() {
        if ($.validate.form()) {
            const answer = $('.summernote-answer').summernote('code');
            $("#answer").val(answer);
            const analysis = $('.summernote-analysis').summernote('code');
            $("#analysis").val(analysis);
            $.operate.save(prefix + "/edit", $('#form-answer-edit').serialize());
        }
    }
</script>
</body>
</html>
